// :stack.cpp
#include "stack.h"

bool InitStack(LinkStack &S)
{
	S.base = (Node *)malloc(sizeof(Node)); //栈底不存放任何数据
	if(!S.base)
		exit(-1);
	S.top = S.base;
	S.base->next = NULL;
	S.stacklength = 0;
	return true;
}

bool DestoryStack(LinkStack &S)
{
	Node *head = S.top;
	while(head!=NULL)
	{
		S.top = S.top->next;
		free(head);
		head = S.top;
	}
	S.base = NULL;
	S.stacklength = 0;
	return true;
}

bool ClearStack(LinkStack &S)
{
	return DestoryStack(S);
}

bool StackEmpty(LinkStack S)
{
	return S.stacklength == 0;
}

int StackLength(LinkStack S)
{
	return S.stacklength;
}

bool GetTop(LinkStack S, LElemType &e)
{
	e = S.top->data;
	return true;
}

bool Push(LinkStack &S, LElemType e)
{
	Node *p = (Node *)malloc(sizeof(Node));
	if(!p)
		exit(-1);
	p->data = e;
	p->next = S.top; 
	S.top = p;
	S.stacklength++;
	return true;
}

bool Pop(LinkStack &S, LElemType &e)
{
	if(S.top == S.base)
		return false;
	e = S.top->data;
	S.top = S.top->next;
	S.stacklength--;

	return true;
	
}

